package com.wolfsea.dottedlinedemo
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.constraintlayout.widget.ConstraintLayout

/**
 *@desc DottedLineLayout
 *@author liuliheng
 *@time 2022/12/7  18:19
 **/
class DottedLineLayout @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null) :
    ConstraintLayout(context, attributeSet) {

    private val circlePaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            style = Paint.Style.STROKE
            strokeWidth = context.resources.getDimension(R.dimen.dp_1)
            color = context.resources.getColor(R.color.color_f44336)
        }
    }

    private val leftCircleRectF by lazy {
        RectF()
    }

    private val rightCircleRectF by lazy {
        RectF()
    }

    private val lineRect by lazy {
        Rect()
    }

    private val path by lazy {
        Path()
    }

    private val linePaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_f44336)
        }
    }

    private val baseHeight = context.resources.getDimension(R.dimen.dp_60).toInt()
    private val lineTop = baseHeight * 3 + baseHeight / 2
    private val lineBottom = lineTop + context.resources.getDimension(R.dimen.dp_1).toInt()

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val viewWidth = context.resources.displayMetrics.widthPixels - paddingStart - paddingEnd
        val viewHeight = context.resources.displayMetrics.heightPixels - paddingTop - paddingBottom
        setMeasuredDimension(viewWidth, viewHeight)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
    }

    override fun dispatchDraw(canvas: Canvas?) {
        canvas?.apply {
            leftCircleRectF.set(
                -baseHeight / 2 * 1.0F,
                baseHeight * 3.0F,
                baseHeight / 2 * 1.0F,
                baseHeight * 4.0F
            )
            //剜掉一部分区域
            path.reset()
            path.addArc(leftCircleRectF,-90f,180f)
            clipOutPath(path)

            rightCircleRectF.set(
                width - baseHeight / 2 * 1.0F,
                baseHeight * 3.0f,
                width + baseHeight / 2 * 1.0F,
                baseHeight * 4.0F
            )
            //剜掉一部分区域
            path.reset()
            path.addArc(rightCircleRectF,90f,270f)
            clipOutPath(path)

            //绘制颜色  相当于设置背景  在此处绘制clipOutPath()才会起作用
            drawColor(context.resources.getColor(R.color.color_009688))

            val leftPieces = BASE_ANGLE / (ARC_WIDTH + ARC_SPACE)
            for (i in 0.until(leftPieces)) {
                val startAngle = i * (ARC_WIDTH + ARC_SPACE).toFloat() - 90
                val sweepAngle = ARC_WIDTH.toFloat()
                drawArc(
                    leftCircleRectF,
                    startAngle,
                    sweepAngle,
                    false,
                    circlePaint
                )
            }

            val rightPieces = BASE_ANGLE / (ARC_WIDTH + ARC_SPACE)
            for (i in 0.until(rightPieces)) {
                val startAngle = i * (ARC_WIDTH + ARC_SPACE).toFloat() + 90
                val sweepAngle = ARC_WIDTH.toFloat()
                drawArc(
                    rightCircleRectF,
                    startAngle,
                    sweepAngle,
                    false,
                    circlePaint
                )
            }

            val remainedWidth = (measuredWidth - baseHeight) % (LINE_WIDTH + LINE_SPACE)
            val lineCount = (measuredWidth - baseHeight - remainedWidth) / (LINE_WIDTH + LINE_SPACE)

            var space = 0
            for (i in 0.until(lineCount)) {
                lineRect.set(
                    LINE_WIDTH * i + space + remainedWidth / 2 + LINE_SPACE / 2 + baseHeight / 2,
                    lineTop,
                    LINE_WIDTH * (i + 1) + space + remainedWidth / 2 + LINE_SPACE / 2 + baseHeight / 2,
                    lineBottom
                )
                drawRect(lineRect, linePaint)
                space += LINE_SPACE
            }
        }
    }

    companion object {
        const val BASE_ANGLE = 180
        const val ARC_WIDTH = 10
        const val ARC_SPACE = 5
        //线宽
        const val LINE_WIDTH = 10
        //线间距
        const val LINE_SPACE = 5
    }

}